package com.product.android.utils;

import java.io.File;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;

import com.common.android.utils.ImageUtils;
import com.common.android.utils.ToastUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SaveTweetImageTask extends AsyncTask<String, Void, Void> {
	private String mUrl;
	private ImageLoader mImageLoader = ImageLoader.getInstance();
	private Context mContext;
	private SavePicHandler mHandler;
	private DisplayImageOptions mOption;
	private String mImageExt;
	

	public SaveTweetImageTask(String url, String image_ext,Context context, DisplayImageOptions option) {
		mUrl = url;
		mContext = context;
		mHandler = new SavePicHandler(context);
		mOption = option;
		mImageExt = image_ext;
	}

	@Override
	protected Void doInBackground(String... params) {
		String path = Environment.getExternalStorageDirectory()
				+ "/DCIM/camera/";
		StringBuilder name = new StringBuilder( mImageLoader.getDiscCacheFileName(
				mOption, mUrl));		
		if(! ImageUtils.isGifForWeibo(mImageExt, mUrl)){
			name.append(".jpg");
		}else{
			name.append(".gif");
		}
		File file = new File(path + name);
		
		Message msg = Message.obtain();
		msg.what = SAVE_IMAGE_START;
		mHandler.sendMessage(msg);
		if (mImageLoader.saveDiscCacheFile(
				mOption, mUrl,
				file)) {
			/**
			 * 以下这块为了图片保存到sd卡后可以立即在图库刷新
			 */
			ContentValues localContentValues = new ContentValues();
			localContentValues.put(Media.DATA, file.toString());
			localContentValues.put("description", "save image ---");
			localContentValues.put(Media.MIME_TYPE, "image/jpeg");
			// 要加上这个时间，否则照片显示会变到最后面
			localContentValues.put(Media.DATE_TAKEN, new Date().getTime());
			ContentResolver localContentResolver = mContext
					.getContentResolver();
			Uri localUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
			localContentResolver.insert(localUri, localContentValues);

			msg = Message.obtain();
			msg.what = SAVE_IMAGE_SUCCESS;
			msg.obj = file.getPath();
			mHandler.sendMessage(msg);
		} else {
			msg = Message.obtain();
			msg.what = SAVE_IMAGE_FAIL;
			mHandler.sendMessage(msg);
		}
		return null;
	}
	
	public static final int SAVE_IMAGE_START = 0;//开始下载图片
	public static final int SAVE_IMAGE_SUCCESS = 1;//图片下载成功
	public static final int SAVE_IMAGE_FAIL = 2;//图片下载失败
	public static final int SAVE_IMAGE_NOT_EXIST = 3;//图片不存在
	class SavePicHandler extends Handler{
		private Context mContext;
		@SuppressLint("HandlerLeak")
		public SavePicHandler(Context context){
			mContext = context;
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SAVE_IMAGE_START:
				ToastUtils.displayTimeShort(mContext, "开始保存图片...");
				break;
			case SAVE_IMAGE_SUCCESS:
				ToastUtils.displayTimeShort(mContext, "保存图片成功！保存路径为:" + (String) msg.obj);
				break;
			case SAVE_IMAGE_FAIL:
				ToastUtils.displayTimeShort(mContext, "保存图片失败！");
				break;
			case SAVE_IMAGE_NOT_EXIST:
				ToastUtils.displayTimeShort(mContext, "图片正在下载中，请稍后再试！");
				break;
			}
		}

	};

}
